﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FlashNut
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private FlashNutReader reader;

        public Window1()
        {
            reader = new FlashNutReader();
            this.DataContext = reader.FlashNutter;
            InitializeComponent();
            listViewDeck.SelectedIndex = 0;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //reader.Save();
        }

        private void AddNewDeckButton_Click(object sender, RoutedEventArgs e)
        {
            if (Validation.GetHasError(NewDeckTextBox))
                return;

            FlashNutterDeck deck = reader.AddDeck(NewDeckTextBox.Text);
            listViewDeck.SelectedItem = deck;
            MainTabControl.SelectedItem = (TabItem)MainTabControl.FindName("TabItemBuild");
        }

        private void DeleteDeck_Click(object sender, RoutedEventArgs e)
        {
            FlashNutterDeck deck = ((sender as Button).Tag as FlashNutterDeck);
            reader.DeleteDeck(deck);
        }

        private void BuildDeck_Click(object sender, RoutedEventArgs e)
        {
            FlashNutterDeck deck = ((sender as Button).Tag as FlashNutterDeck);
            listViewDeck.SelectedItem = deck;
            MainTabControl.SelectedItem = (TabItem)MainTabControl.FindName("TabItemBuild");
        }

        private void ReviewDeck_Click(object sender, RoutedEventArgs e)
        {
            FlashNutterDeck deck = ((sender as Button).Tag as FlashNutterDeck);
            listViewDeck.SelectedItem = deck;
            MainTabControl.SelectedItem = (TabItem)MainTabControl.FindName("TabItemReview");
        }

        private void DeleteFlash_Click(object sender, RoutedEventArgs e)
        {
            FlashNutterDeck deck = (FlashNutterDeck)TabItemBuild.DataContext;
            FlashNutterDeckFlash flash = ((sender as Button).Tag as FlashNutterDeckFlash);
            reader.DeleteFlash(deck, flash);
        }

        private void AddFlash_Click(object sender, RoutedEventArgs e)
        {
            if (Validation.GetHasError(QuestionTextBox) || Validation.GetHasError(AnswerTextBox))
                return;
            
            FlashNutterDeck deck = ((sender as Button).Tag as FlashNutterDeck);
            reader.AddFlash(deck, QuestionTextBox.Text, AnswerTextBox.Text);
            QuestionTextBox.Clear();
            QuestionTextBox.Focus();
            AnswerTextBox.Clear();
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            FlashNutterDeck deck = (FlashNutterDeck)TabItemBuild.DataContext;
            FlashNutterDeckFlash flash = ((sender as Button).Tag as FlashNutterDeckFlash);
        }

        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            FlashNutterDeck deck = (FlashNutterDeck)TabItemBuild.DataContext;
            FlashNutterDeckFlash flash = ((sender as Button).Tag as FlashNutterDeckFlash);
            reader.NextFlash(deck, flash);
        }

        private void ResetReview_Click(object sender, RoutedEventArgs e)
        {
            FlashNutterDeck deck = (FlashNutterDeck)TabItemBuild.DataContext;
            reader.ResetReview(deck);
            listViewReview.SelectedIndex = 0;
        }

        private void OpenCmdExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            string f = Util.OpenDialog();
            if (f != null) {
                this.DataContext = null;
                reader.Open(f);
                this.DataContext = reader.FlashNutter;
            }
        }

        private void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void NewCmdExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            this.DataContext = null;
            reader.New();
            this.DataContext = reader.FlashNutter;
        }

        private void NewCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void SaveCmdExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            string f = Util.SaveDialog();
            if (f != null)
                reader.Save(f);
        }

        private void SaveCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void CloseCmdExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            string f = Util.SaveDialog();
            if (f != null)
            {
                reader.Save(f);
                Application.Current.Shutdown();
            }
        }

        private void CloseCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    }
}
